package cn.neusoft.controller;

import cn.neusoft.po.Department;
import cn.neusoft.query.DepartmentQuery;
import cn.neusoft.service.IDepartmentService;
import cn.neusoft.vo.ResponseObj;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author hxj
 * @since 2023-12-19
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    IDepartmentService service;

    @PostMapping
    public ResponseObj saveOrUpdate(@RequestBody Department department){
        boolean b = service.saveOrUpdate(department);
        return b?ResponseObj.SUCCESS():ResponseObj.ERROR(10001,"保存部门信息失败");
    }

    @DeleteMapping("/{id}")
    public ResponseObj delete(@PathVariable("id") Integer id){
        boolean b = service.removeById(id);
        return b?ResponseObj.SUCCESS():ResponseObj.ERROR(10002,"删除部门信息失败");
    }

    @GetMapping("/{id}")
    public ResponseObj getById(@PathVariable("id") Integer id){
        Department department = service.getById(id);
        return ResponseObj.SUCCESS(department);
    }

    @PostMapping("/page")
    public ResponseObj getByPage(@RequestBody DepartmentQuery query){
        //构建条件搜索对象
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.hasText(query.getDname()),"name",query.getDname());

        //构建分页条件对象
        Page pageQuery = new Page(query.getCurrent(),query.getSize());
        Page<Department> page = service.page(pageQuery, queryWrapper);
        return ResponseObj.SUCCESS(page);
    }

    @GetMapping("/all")
    public ResponseObj getAll(){
        List<Department> list = service.list();
        return ResponseObj.SUCCESS(list);
    }

}
